import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by losor on 2017/3/18.
 */
public class _225_InplementStackUsingQueues {



}

class MyStack {


    Queue<Integer> queue;
    boolean needfind = false;
    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedBlockingQueue<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        queue.add(x);
        needfind = true;
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if (needfind)
            find();
        needfind = true;
        return queue.poll();
    }

    private void find(){
        int count = queue.size();
        for (int i = 0; i < count - 1; i++) {
            queue.add(queue.poll());
        }
    }

    /** Get the top element. */
    public int top() {
        if (needfind)
            find();
        needfind = false;
        return queue.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}
